package com.jinken._02global;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.List;

/**
 * 生产者
 */
public class Producer {
    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        //创建生产者对象
        DefaultMQProducer dmp = new DefaultMQProducer("global_group");
        //设置NamseServer地址
        dmp.setNamesrvAddr("192.168.126.100:9876");
        //启动生产者
        dmp.start();

        for (int i = 1; i <= 10; i++) {
            //创建消息对象
            Message message = new Message("global_topic","",("hello mq golabel "+i).getBytes());

            //发送消息
            SendResult result = dmp.send(message, new MessageQueueSelector() {
                /**
                 * 队列选择
                 * @param list 队列集合
                 * @param message 消息
                 * @param o  外层 send方法的第三个参数
                 * @return
                 */
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
                    System.out.println("list = " + list);
                    System.out.println("message = " + message);
                    System.out.println("o = " + o);

                    return list.get(1);
                }
            },1);

        }

        //关闭生产者
        dmp.shutdown();
    }


}
